package pta.test05.test06;

import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        String[] arr1 = new String[n];
        String[] arr2 = new String[n];
        for (int i = 0; i < n; i++) {
            String sc1 = scanner.nextLine();
            arr1[i]=sc1;
            arr2[i]=sc1.substring(6,14);
        }
        while (true){
            String sc3 = scanner.nextLine();
            if (sc3.equals("e")){
                System.out.println("exit");
                break;
            }
            if (sc3.equals("sort1")){
                Arrays.sort(arr2);
                for (int i = 0; i < n; i++) {
                    System.out.println(arr2[i].substring(0,4)+'-'+arr2[i].substring(4,6)+'-'+arr2[i].substring(6,8));

                }
            } else if (sc3.equals("sort2")) {
                Arrays.sort(arr2);
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        if (arr1[j].contains(arr2[i])){
                            System.out.println(arr1[j]);
                            break;
                        }
                    }
                }
            }
        }

    }
}
